package view.waiters;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

import com.eltima.components.ui.DatePicker;

import entiey.Waiter;
import service.WaiterService;
import service.impl.WaiterServiceImpl;
import util.CastUtil;

import javax.swing.JCheckBoxMenuItem;
import javax.swing.JPopupMenu;
import java.awt.Component;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JMenuItem;
import javax.swing.JOptionPane;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;

public class waiter extends JFrame {

	private JPanel contentPane;
	private JTextField text_Id;
	private JTextField text_Name;
	private JTextField text_Gender;
	private JTextField text_Phone;
	private JTextField text_Time;
	private JTable table;
	private JTextField text_Search_id;
	private JTextField text_SearchName;
	private JTextField textField_2;
	private DatePicker dp;
	private JTextPane text_MoreInfo;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				
				try {
					waiter frame = new waiter();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public waiter() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 846, 569);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		JLabel lblNewLabel = new JLabel("服务员信息管理");
		lblNewLabel.setFont(new Font("宋体", Font.BOLD, 18));
		
		JPanel panel = new JPanel();
		
		JLabel lblNewLabel_4 = new JLabel("添加员工");
		lblNewLabel_4.setFont(new Font("宋体", Font.BOLD, 14));
		
		JLabel lblNewLabel_5 = new JLabel("工 号：");
		
		JLabel lblNewLabel_6 = new JLabel("姓 名：");
		
		JLabel lblNewLabel_7 = new JLabel("性 别：");
		
		JLabel lblNewLabel_8 = new JLabel("电 话：");
		
		JLabel lblNewLabel_9 = new JLabel("入职时间：");
		
		JLabel lblNewLabel_10 = new JLabel("备 注：");
		
		JButton btnNewButton_2 = new JButton("确认");
		btnNewButton_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				WaiterService waService = new WaiterServiceImpl();
				
				
				
				Waiter w = new Waiter();
				w.setWaiterNo(Integer.parseInt(text_Id.getText()));
				w.setName(text_Name.getText());
				w.setGender(text_Gender.getText());
				w.setPhone(text_Phone.getText());
				String InTime = dp.getInnerTextField().getText();
				try {
					w.setTime(CastUtil.parse(InTime));
				} catch (ParseException e1) {
					e1.printStackTrace();
				}
				
				w.setMoreInfo(text_MoreInfo.getText());
				
				if(waService.saveWaiter(w)) {
					JOptionPane.showMessageDialog(waiter.this, "保存成功");
//					dispose();//关闭当前窗体
					initTable();
					text_Id.setText("");
					text_Name.setText("");
					text_Gender.setText("");
					text_Phone.setText("");
					text_MoreInfo.setText("");
				}else {
					JOptionPane.showMessageDialog(waiter.this, "保存失败");
				}
				
			}
		});
		
		JButton btnNewButton_3 = new JButton("取消");
		btnNewButton_3.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				text_Id.setText("");
				text_Name.setText("");
				text_Gender.setText("");
				text_Phone.setText("");
				text_MoreInfo.setText("");
//				text_Time.setText("");
			}
		});
		
		text_Id = new JTextField();
		text_Id.setColumns(10);
		
		text_Name = new JTextField();
		text_Name.setColumns(10);
		
		text_Gender = new JTextField();
		text_Gender.setColumns(10);
		
		text_Phone = new JTextField();
		text_Phone.setColumns(10);
		
		
		
		dp = new DatePicker();
		
		text_MoreInfo = new JTextPane();
		
		JScrollPane scrollPane = new JScrollPane();
		
		table = new JTable();
		table.setModel(new DefaultTableModel(
			new Object[][] {
			},
			new String[] {
				"\u7F16\u53F7", "\u5DE5\u53F7", "\u59D3\u540D", "\u6027\u522B", "\u7535\u8BDD", "\u5165\u804C\u65F6\u95F4", "\u5907\u6CE8"
			}
		));
		scrollPane.setViewportView(table);
		
		JPopupMenu popupMenu = new JPopupMenu();
		popupMenu.setLabel("");
		addPopup(table, popupMenu);
		
		JMenuItem mntmNewMenuItem = new JMenuItem("修改");
		popupMenu.add(mntmNewMenuItem);
		
		JMenuItem mntmNewMenuItem_1 = new JMenuItem("删除");
		popupMenu.add(mntmNewMenuItem_1);
		
		JCheckBoxMenuItem checkBoxMenuItem = new JCheckBoxMenuItem("New check item");
		scrollPane.setColumnHeaderView(checkBoxMenuItem);
		
		JLabel lblNewLabel_11 = new JLabel("员工信息展示");
		lblNewLabel_11.setFont(new Font("宋体", Font.BOLD, 14));
		
		JPanel panel_1 = new JPanel();
		panel_1.setLayout(null);
		
		JLabel lblNewLabel_1 = new JLabel("工 号：");
		lblNewLabel_1.setBounds(44, 26, 54, 15);
		panel_1.add(lblNewLabel_1);
		
		text_Search_id = new JTextField("0");
		text_Search_id.setColumns(10);
		text_Search_id.setBounds(108, 23, 75, 21);
		panel_1.add(text_Search_id);
		
		JLabel lblNewLabel_2 = new JLabel("姓 名：");
		lblNewLabel_2.setBounds(255, 26, 54, 15);
		panel_1.add(lblNewLabel_2);
		
		text_SearchName = new JTextField();
		text_SearchName.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			}
		});
		text_SearchName.setColumns(10);
		text_SearchName.setBounds(332, 23, 80,20);
		panel_1.add(text_SearchName);
		
				
		JButton btn_search = new JButton("查 询");
		btn_search.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				
				int  id = Integer.parseInt(text_Search_id.getText().trim());
				
				String value = text_SearchName.getText();
				
				WaiterService ws = new WaiterServiceImpl();
				
				List<Waiter> list = new ArrayList<Waiter>();
//				list.add(ws.findById(id));
				if(id==0&!(value.equals(""))) {
					list=ws.query(value);
				}else if(id!=0&!(value.equals(""))){
					list=ws.query(value);
					list.add(ws.findById(id));
				}else if( id!=0&(value.equals("")) ) {
					Waiter a= ws.findById(id);
					list.add(a);
				}
				
				
				initTable(list);
			}
		});
		btn_search.setBounds(526, 22, 93, 23);
		panel_1.add(btn_search);
		
		JButton btn_cancelSearch = new JButton("取 消");
		btn_cancelSearch.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				text_SearchName.setText("");
				text_Search_id.setText("0");
				initTable();
				
			}
		});
		btn_cancelSearch.setBounds(667, 22, 93, 23);
		panel_1.add(btn_cancelSearch);
		
//		JButton_1.setBounds(671, 22, 93, 23);
//		panel_1.add(btnNewButton_1);
		
		JPanel panel_2 = new JPanel();
		panel_2.setLayout(null);
		
		JButton btnNewButton_4 = new JButton("添加");
		btnNewButton_4.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				waiter_add_update addWaiter = new waiter_add_update();
				addWaiter.setBounds(waiter.this.getX(), waiter.this.getY(), addWaiter.getWidth(), addWaiter.getHeight());
				addWaiter.setVisible(true);
				
				//监听添加员工的窗体是否被关闭   ,适配器设计模式
				addWaiter.addWindowListener(new WindowAdapter() {
					
					@Override
					public void windowClosed(WindowEvent e) {
						initTable();
						//当窗体关闭，初始化服务员表格 (重新获取所有服务员)
					}
					
					@Override
					public void windowClosing(WindowEvent e) {
					}
				});
				
				
				
			}
		});
		btnNewButton_4.setBounds(0, 0, 93, 23);
		panel_2.add(btnNewButton_4);
		
		JButton btnNewButton_5 = new JButton("删除");
		btnNewButton_5.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int index = table.getSelectedRow();//选择的行数
				if(index>=0) {
					int n = JOptionPane.showConfirmDialog(waiter.this, "确定删除"+table.getValueAt(index, 1)+"吗？","删除",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE);
					if(n==0) {
						
						Object v = table.getValueAt(index, 0);//第0列 ，删除id
						int id = CastUtil.castInt(v);
						WaiterService service = new WaiterServiceImpl();
						if(service.deleteWaiter(id)) {
							JOptionPane.showMessageDialog(waiter.this, "删除成功");
							initTable();
						}else {
							JOptionPane.showMessageDialog(waiter.this, "删除失败");
							
						}
						
					}
					
				}else {
					JOptionPane.showMessageDialog(waiter.this,"未选中,请选择要删除的服务员" );
				}
			}
		});
		
		btnNewButton_5.setBounds(231, 0, 93, 23);
		panel_2.add(btnNewButton_5);
		
		JButton btnNewButton_6 = new JButton("修改");
		btnNewButton_6.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				int index = table.getSelectedRow(); //获取选中的行数
				if(index>=0) {
					Object v = table.getValueAt(index, 1);//获取选中的行数下标的
					int id = CastUtil.castInt(v);  //将 转成int行数
					
					waiter_add_update updatewaiter = new waiter_add_update(id);
					updatewaiter.setBounds(waiter.this.getX(), waiter.this.getY(), updatewaiter.getWidth(), updatewaiter.getHeight());
					updatewaiter.setVisible(true);
					
					//监听添加员工的窗体是否被关闭   ,适配器设计模式
					updatewaiter.addWindowListener(new WindowAdapter() {
						
						@Override
						public void windowClosed(WindowEvent e) {
							initTable();
							//当窗体关闭，初始化服务员表格 (重新获取所有服务员)
						}
						
						@Override
						public void windowClosing(WindowEvent e) {
						}
					});
					
				}else {
					JOptionPane.showMessageDialog(waiter.this, "未选中,请选择服务员");
				}
				
			}
		});
		btnNewButton_6.setBounds(111, 0, 93, 23);
		panel_2.add(btnNewButton_6);
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGap(5)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGap(318)
							.addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 180, GroupLayout.PREFERRED_SIZE))
						.addComponent(panel_1, GroupLayout.DEFAULT_SIZE, 802, Short.MAX_VALUE)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addComponent(panel, GroupLayout.PREFERRED_SIZE, 244, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
								.addGroup(gl_contentPane.createSequentialGroup()
									.addGap(229)
									.addComponent(lblNewLabel_11, GroupLayout.PREFERRED_SIZE, 120, GroupLayout.PREFERRED_SIZE))
								.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 564, Short.MAX_VALUE)
								.addGroup(gl_contentPane.createSequentialGroup()
									.addGap(94)
									.addComponent(panel_2, GroupLayout.DEFAULT_SIZE, 353, Short.MAX_VALUE)
									.addGap(117)))))
					.addGap(15))
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGap(5)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 42, GroupLayout.PREFERRED_SIZE)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGap(32)
							.addComponent(panel_1, GroupLayout.DEFAULT_SIZE, 67, Short.MAX_VALUE)))
					.addGap(6)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addComponent(lblNewLabel_11, GroupLayout.PREFERRED_SIZE, 15, GroupLayout.PREFERRED_SIZE)
							.addGap(10)
							.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 330, GroupLayout.PREFERRED_SIZE)
							.addGap(9)
							.addComponent(panel_2, GroupLayout.PREFERRED_SIZE, 26, GroupLayout.PREFERRED_SIZE))
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGap(4)
							.addComponent(panel, GroupLayout.DEFAULT_SIZE, 386, Short.MAX_VALUE)))
					.addGap(22))
		);
		GroupLayout gl_panel = new GroupLayout(panel);
		gl_panel.setHorizontalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addGap(80)
					.addComponent(lblNewLabel_4, GroupLayout.PREFERRED_SIZE, 60, GroupLayout.PREFERRED_SIZE))
				.addGroup(gl_panel.createSequentialGroup()
					.addGap(27)
					.addComponent(lblNewLabel_5, GroupLayout.PREFERRED_SIZE, 54, GroupLayout.PREFERRED_SIZE)
					.addGap(22)
					.addComponent(text_Id, GroupLayout.DEFAULT_SIZE, 113, Short.MAX_VALUE)
					.addGap(7))
				.addGroup(gl_panel.createSequentialGroup()
					.addGap(27)
					.addComponent(lblNewLabel_6, GroupLayout.PREFERRED_SIZE, 54, GroupLayout.PREFERRED_SIZE)
					.addGap(22)
					.addComponent(text_Name, GroupLayout.DEFAULT_SIZE, 113, Short.MAX_VALUE)
					.addGap(7))
				.addGroup(gl_panel.createSequentialGroup()
					.addGap(27)
					.addComponent(lblNewLabel_7, GroupLayout.PREFERRED_SIZE, 54, GroupLayout.PREFERRED_SIZE)
					.addGap(22)
					.addComponent(text_Gender, GroupLayout.DEFAULT_SIZE, 113, Short.MAX_VALUE)
					.addGap(7))
				.addGroup(gl_panel.createSequentialGroup()
					.addGap(27)
					.addComponent(lblNewLabel_8, GroupLayout.PREFERRED_SIZE, 54, GroupLayout.PREFERRED_SIZE)
					.addGap(22)
					.addComponent(text_Phone, GroupLayout.DEFAULT_SIZE, 113, Short.MAX_VALUE)
					.addGap(7))
				.addGroup(gl_panel.createSequentialGroup()
					.addGap(27)
					.addComponent(lblNewLabel_9, GroupLayout.PREFERRED_SIZE, 65, GroupLayout.PREFERRED_SIZE)
					.addGap(11)
					.addComponent(dp, GroupLayout.DEFAULT_SIZE, 113, Short.MAX_VALUE)
					.addGap(7))
				.addGroup(gl_panel.createSequentialGroup()
					.addGap(27)
					.addComponent(lblNewLabel_10, GroupLayout.PREFERRED_SIZE, 54, GroupLayout.PREFERRED_SIZE)
					.addGap(19)
					.addComponent(text_MoreInfo, GroupLayout.DEFAULT_SIZE, 116, Short.MAX_VALUE)
					.addGap(7))
				.addGroup(gl_panel.createSequentialGroup()
					.addGap(27)
					.addComponent(btnNewButton_2, GroupLayout.DEFAULT_SIZE, 60, Short.MAX_VALUE)
					.addGap(49)
					.addComponent(btnNewButton_3, GroupLayout.DEFAULT_SIZE, 65, Short.MAX_VALUE)
					.addGap(22))
		);
		gl_panel.setVerticalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addComponent(lblNewLabel_4)
					.addGap(32)
					.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel.createSequentialGroup()
							.addGap(3)
							.addComponent(lblNewLabel_5))
						.addComponent(text_Id, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(17)
					.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel.createSequentialGroup()
							.addGap(3)
							.addComponent(lblNewLabel_6))
						.addComponent(text_Name, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(32)
					.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel.createSequentialGroup()
							.addGap(3)
							.addComponent(lblNewLabel_7))
						.addComponent(text_Gender, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(28)
					.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel.createSequentialGroup()
							.addGap(3)
							.addComponent(lblNewLabel_8))
						.addComponent(text_Phone, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(29)
					.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel.createSequentialGroup()
							.addGap(3)
							.addComponent(lblNewLabel_9))
						.addComponent(dp, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(17)
					.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel.createSequentialGroup()
							.addGap(6)
							.addComponent(lblNewLabel_10))
						.addComponent(text_MoreInfo, GroupLayout.PREFERRED_SIZE, 52, GroupLayout.PREFERRED_SIZE))
					.addGap(25)
					.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
						.addComponent(btnNewButton_2)
						.addComponent(btnNewButton_3)))
		);
		panel.setLayout(gl_panel);
		contentPane.setLayout(gl_contentPane);
		
		initTable();
		
	}
	private static void addPopup(Component component, final JPopupMenu popup) {
		component.addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}
			public void mouseReleased(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}
			private void showMenu(MouseEvent e) {
				popup.show(e.getComponent(), e.getX(), e.getY());
			}
		});
	}
	
	//初始化服务员表格
	public void initTable() {
		DefaultTableModel dtm = (DefaultTableModel) table.getModel();
		
		//获取表中行数   ，全部删除
		int count = dtm.getRowCount();
		for(int i=0;i<count;i++) {
			dtm.removeRow(0);
		}
		
		WaiterService waService = new WaiterServiceImpl();
		List<Waiter> list = waService.findAllWaiter();
		for(Waiter w : list) {
			dtm.addRow(new Object[] {
				w.getId(),
				w.getWaiterNo(),
				w.getName(),
				w.getGender(),
				w.getPhone(),
				w.getTime(),
				w.getMoreInfo()
			});
		}
		
	}
	
	
	
	public void initTable(List<Waiter> list) {
		DefaultTableModel dtm = (DefaultTableModel) table.getModel();
		
		//获取表中行数   ，全部删除
		int count = dtm.getRowCount();
		for(int i=0;i<count;i++) {
			dtm.removeRow(0);
		}
		
//		WaiterService waService = new WaiterServiceImpl();
//		l = waService.findAllWaiter();
		for(Waiter w : list) {
			dtm.addRow(new Object[] {
				w.getId(),
				w.getWaiterNo(),
				w.getName(),
				w.getGender(),
				w.getPhone(),
				w.getTime(),
				w.getMoreInfo()
			});
		}
		
	}
	
	
	
	
	
	
}
